package com.patrol.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.patrol.dao.WorkDao;
import com.patrol.dao.PatrolDao;
import com.patrol.pojo.Patrol;
import com.patrol.service.PatrolService;
import com.patrol.utils.DownloadSettingUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Service
public class PatrolServiceImpl implements PatrolService {

    @Resource
    private PatrolDao patrolDao;

    @Resource
    private WorkDao workDao;

    @Override
    public Page<Patrol> selectPage(long page, long limit) {
        return patrolDao.selectPage(new Page<>(page, limit), null);
    }

    @Override
    public void exportDataByDate(HttpServletResponse response, String startTime, String endTime) {
        DownloadSettingUtil.setting(response, "patrol-data");
        QueryWrapper<Patrol> qw = new QueryWrapper<>();
        qw.between("date", startTime, endTime);
        List<Patrol> patrols = patrolDao.selectList(qw);
        try {
            EasyExcel.write(response.getOutputStream(), Patrol.class).sheet("data").doWrite(patrols);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
